# @Time   : 2022-03-03 17:00:00
# @Author : Gavin Jiang
# @Owner  : YSRD (Insights Value)

import os

from framework.the_path import ThePath
from framework.environment import Environment


class ConfigLoader:

    def __init__(self, env=None):
        # env的优先级，参数 > Dockerfile中的ENV
        # self.env = os.getenv('ENVIRONMENT', 'prod') if not env else env
        self.env = Environment.env() if not env else env
        # 统一从配置文件里读，摒弃获取环境变量和start_up直接赋予参数的方式的方式?
        self._data = {
            'log': ThePath.config(['default', 'log.yml']),
            'flask': ThePath.config(['api', self.env, 'flask.py'])
        }

    def load(self):
        env_log_path = ThePath.config(['api', self.env, 'log.yml'])
        if os.path.exists(env_log_path):
            self._data['log'] = env_log_path

        env_flask_path = ThePath.config(['api', self.env, 'flask.py'])
        if os.path.exists(env_flask_path):
            self._data['flask'] = env_flask_path

    @property
    def data(self):
        return self._data
